<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="itemNo">
        <el-input
          v-model="queryParams.itemNo"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="itemType">
        <el-select v-model="queryParams.itemType" placeholder="请选择分类" clearable>
          <el-option
            v-for="dict in dict.type.wms_item_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位类别" prop="unit">
        <el-select v-model="queryParams.unit" placeholder="请选择单位类别" clearable>
          <el-option
            v-for="dict in dict.type.wms_item_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择所属仓库" clearable @change="handleWarehouseChange">
          <el-option
            v-for="warehouse in warehousePullData"
            :key="warehouse.id"
            :label="warehouse.warehouseName"
            :value="warehouse.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属库区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择所属库区" clearable @change="handleAreaChange">
          <el-option
            v-for="area in filteredAreaData"
            :key="area.id"
            :label="area.areaName"
            :value="area.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属货架" prop="rackId">
        <el-select v-model="queryParams.rackId" placeholder="请选择所属货架" clearable>
          <el-option
            v-for="rack in filteredRackData"
            :key="rack.id"
            :label="rack.rackName"
            :value="rack.id"
          ></el-option>
        </el-select>
      </el-form-item>

<!--      <el-form-item label="安全库存" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入安全库存"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效期" prop="expiryDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.expiryDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择有效期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="唯一值" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" width="50" align="center" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="itemNo" />
      <el-table-column label="名称" align="center" prop="itemName" />
      <el-table-column label="分类" align="center" prop="itemType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_item_type" :value="scope.row.itemType"/>
        </template>
      </el-table-column>
      <el-table-column label="单位类别" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_item_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="所属货架" align="center" prop="rackId">
        <template slot-scope="scope">
          <rack-tag :options="rackPullData" :value="scope.row.rackId"/>
        </template>
      </el-table-column>
      <el-table-column label="所属库区" align="center" prop="areaId">
        <template slot-scope="scope">
          <area-tag :options="areaPullData" :value="scope.row.areaId"/>
        </template>
      </el-table-column>

      <el-table-column label="所属仓库" align="center" prop="warehouseId">
        <template slot-scope="scope">
          <warehouse-tag :options="warehousePullData" :value="scope.row.warehouseId"/>
        </template>
      </el-table-column>
      <el-table-column label="安全库存" align="center" prop="quantity" />
      <el-table-column label="有效期" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编号" align="center" prop="deviceNumber" />
      <el-table-column label="人员数量" align="center" prop="headcount" />
      <el-table-column label="库容" align="center" prop="storageCapacity" />
      <el-table-column label="罐组" align="center" prop="tankGroup" />
      <el-table-column label="可燃气体探头数量" align="center" prop="probeNumber" />
      <el-table-column label="供油模式" align="center" prop="oilSupplyMode" />
      <el-table-column label="油源" align="center" prop="oilSource" />
      <el-table-column label="实验室" align="center" prop="laboratory" />
      <el-table-column label="消防能力(罐、泵)" align="center" prop="fireFightingCapability" />
      <el-table-column label="加油车名称" align="center" prop="vehicleName" />
      <el-table-column label="加油车型号" align="center" prop="vehicleModel" />
      <el-table-column label="加油车制造厂家" align="center" prop="manufacturer" />
      <el-table-column label="加油车底盘型号" align="center" prop="chassisSpecification" />
      <el-table-column label="加油车底盘号" align="center" prop="chassisNumber" />
      <el-table-column label="加油车自编号" align="center" prop="vehicleNo" />
      <el-table-column label="加油车出厂日期" align="center" prop="productionDate" />
      <el-table-column label="加油车出厂编号" align="center" prop="factoryNumber" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1600px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12" >
        <el-form-item label="编号" prop="itemNo">
          <el-input v-model="form.itemNo" placeholder="请输入编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="分类" prop="itemType">
          <el-select v-model="form.itemType" placeholder="请选择分类">
            <el-option
              v-for="dict in dict.type.wms_item_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="单位类别" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择单位类别">
            <el-option
              v-for="dict in dict.type.wms_item_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择所属仓库" @change="handleWarehouseFormChange">
            <el-option
              v-for="warehouse in warehouseFormData"
              :key="warehouse.id"
              :label="warehouse.warehouseName"
              :value="warehouse.id"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="所属库区" prop="areaId">
          <el-select v-model="form.areaId" placeholder="请选择所属库区" @change="handleAreaFormChange">
            <el-option
              v-for="area in filteredFormAreaData"
              :key="area.id"
              :label="area.areaName"
              :value="area.id"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="所属货架" prop="rackId">
          <el-select v-model="form.rackId" placeholder="请选择所属货架">
            <el-option
              v-for="rack in filteredFormRackData"
              :key="rack.id"
              :label="rack.rackName"
              :value="rack.id"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="安全库存" prop="quantity">
          <el-input-number v-model="form.quantity" placeholder="请输入安全库存" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="有效期" prop="expiryDate">
          <el-date-picker clearable
            v-model="form.expiryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="设备编号" prop="deviceNumber">
          <el-input v-model="form.deviceNumber" placeholder="请输入设备编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="人员数量" prop="headcount">
          <el-input-number v-model="form.headcount" placeholder="请输入人员数量" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="库容" prop="storageCapacity">
          <el-input-number :min="0" v-model="form.storageCapacity" placeholder="请输入库容" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="罐组" prop="tankGroup">
          <el-input v-model="form.tankGroup" placeholder="请输入罐组" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="可燃气体探头数量" prop="probeNumber">
          <el-input-number :min="0" v-model="form.probeNumber" placeholder="请输入可燃气体探头数量" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="供油模式" prop="oilSupplyMode">
          <el-input v-model="form.oilSupplyMode" placeholder="请输入供油模式" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="油源" prop="oilSource">
          <el-input v-model="form.oilSource" placeholder="请输入油源" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="实验室" prop="laboratory">
          <el-input v-model="form.laboratory" placeholder="请输入实验室" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="消防能力(罐、泵)" prop="fireFightingCapability">
          <el-input v-model="form.fireFightingCapability" placeholder="请输入消防能力" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="加油车名称" prop="vehicleName">
          <el-input v-model="form.vehicleName" placeholder="请输入加油车名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="加油车型号" prop="vehicleModel">
          <el-input v-model="form.vehicleModel" placeholder="请输入加油车型号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="加油车制造厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入加油车制造厂家" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="加油车底盘型号" prop="chassisSpecification">
          <el-input v-model="form.chassisSpecification" placeholder="请输入加油车底盘型号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="加油车底盘号" prop="chassisNumber">
          <el-input v-model="form.chassisNumber" placeholder="请输入加油车底盘号" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="加油车自编号" prop="vehicleNo">
          <el-input v-model="form.vehicleNo" placeholder="请输入加油车自编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="加油车出厂日期" prop="productionDate">
          <el-date-picker v-model="form.productionDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出厂日期" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
        <el-form-item label="加油车出厂编号" prop="factoryNumber">
          <el-input v-model="form.factoryNumber" placeholder="请输入加油车出厂编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/wms/item";
import {pullWarehouse} from "@/api/wms/warehouse";
import {pullArea} from "@/api/wms/area";
import {pullRack} from "@/api/wms/rack";

export default {
  name: "Item",
  dicts: ['wms_item_type', 'sys_user_sex', 'wms_item_unit'],
  data() {
    return {
      itemUpdate:{},
      filteredAreaData: [],  // 过滤后的库区数据
      filteredRackData: [] ,  // 过滤后的货架数据
      filteredFormAreaData: [],  // 过滤后的库区数据
      filteredFormRackData: [] ,  // 过滤后的货架数据
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemNo: null,
        itemName: null,
        itemType: null,
        unit: null,
        rackId: null,
        areaId: null,
        warehouseId: null,
        quantity: null,
        expiryDate: null,
      },
      warehousePullData: [],
      areaPullData: [],
      rackPullData: [],

      warehouseFormData: [],
      areaFormData: [],
      rackFormData: [],



      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemNo: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        itemName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        unit: [
          { required: true, message: "单位类别不能为空", trigger: "change" }
        ],
        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    /** 查询仓库下拉数据 **/
    pullWarehouse().then(response => {
      this.warehousePullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullArea().then(response => {
      this.areaPullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullRack().then(response => {
      this.rackPullData = response.data;
    });

  },

  methods: {
    handleWarehouseChange() {
      // 当仓库改变时，重置库区和货架选择，并过滤库区数据
      this.queryParams.areaId = null;
      this.queryParams.rackId = null;

      // 根据选中的仓库过滤库区
      this.filteredAreaData = this.areaPullData.filter(area => area.warehouseId === this.queryParams.warehouseId);

      // 清空货架数据
      this.filteredRackData = [];
    },
    handleAreaChange() {
      // 当库区改变时，重置货架选择，并过滤货架数据
      this.queryParams.rackId = null;

      // 根据选中的库区过滤货架
      this.filteredRackData = this.rackPullData.filter(rack => rack.areaId === this.queryParams.areaId);
    },


    handleWarehouseFormChange() {
      // 当仓库改变时，重置库区和货架选择，并过滤库区数据
      this.form.areaId = null;
      this.form.rackId = null;

      // 根据选中的仓库过滤库区
      this.filteredFormAreaData = this.areaFormData.filter(area => area.warehouseId === this.form.warehouseId);

      // 清空货架数据
      this.filteredRackData = [];
    },
    handleAreaFormChange() {
      // 当库区改变时，重置货架选择，并过滤货架数据
      this.form.rackId = null;

      // 根据选中的库区过滤货架
      this.filteredFormRackData = this.rackFormData.filter(rack => rack.areaId === this.form.areaId);
    },


    /** 查询物料列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemNo: null,
        itemName: null,
        itemType: null,
        unit: null,
        rackId: null,
        areaId: null,
        warehouseId: null,
        quantity: null,
        expiryDate: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        probeNumber:0,
        storageCapacity:0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;

      pullWarehouse().then(response => {
        this.warehouseFormData = response.data;
      });

      /** 查询库区下拉数据 **/
      pullArea().then(response => {
        this.areaFormData = response.data;
      });

      /** 查询库区下拉数据 **/
      pullRack().then(response => {
        this.rackFormData = response.data;
      });



      this.title = "添加物料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料";

        pullWarehouse().then(response => {
          this.warehouseFormData = response.data;
        });

        /** 查询库区下拉数据 **/
        pullArea().then(response => {
          this.areaFormData = response.data;
          // 根据选中的仓库过滤库区
          console.log(this.form)
          this.filteredFormAreaData = this.areaFormData.filter(area => area.warehouseId === this.form.warehouseId);
        });

        /** 查询库区下拉数据 **/
        pullRack().then(response => {
          this.rackFormData = response.data;
          // 根据选中的仓库过滤库区
          this.filteredFormRackData = this.rackFormData.filter(rack => rack.areaId === this.form.areaId);
        });

      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
